﻿@using QLNS
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Home.cshtml";
    List<BangNhomNgach> listNG = ViewBag.NhomNgach;
    
}


<div class="toolbar">
<ul>
    <li><input class="tbtnBack" type="button" value="Quay về" onclick="javascript:history.back()"/>
    </li>
     @{ if (Session["Quyen"].ToString() == "1")
        {
    <li><input class="tbtnAdd" type="button" value="Thêm mới" onclick="javascript:window.location='/Gtur/Add/'"/>
    </li>}}
</ul>
<div class="ttitle">Nhóm Ngạch</div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        // prepare the data
        var source =
            {
                datatype: "json",
                datafields: [
                     { name: 'ID' },
                     { name: 'NhomNgach' },
                     { name: 'Loai' },
                     { name: 'HeSoGoc' },
                     { name: 'HeSoTang' },
                     { name: 'SoBac' },
                     { name: 'SoNamTangLuong' }
                ],
                id: 'ID',
                url: '/Gtur/GetPagingGrid/',
                root: 'Rows',
                sort: function () {
                    // update the grid and send a request to the server.
                    $("#jqxgridcontent").jqxGrid('updatebounddata', 'sort');
                }

            };

        var dataAdapter = new $.jqx.dataAdapter(source,
                {
                    downloadComplete: function (data) {
                        source.totalrecords = data.TotalRows;
                    }
                }
            );

        // initialize jqxGrid
        $("#jqxgridcontent").jqxGrid(
            {
                width: '100%',
                source: dataAdapter,
                theme: 'darkblue',
                autoheight: true,
                pageable: true,
                virtualmode: true,
                sortable: true,
                rendergridrows: function (params) {
                    return params.data;
                },
                ready: function () {
                    $("#jqxgridcontent").jqxGrid('hidecolumn', 'ID');
                     @{ if (Session["Quyen"].ToString() == "2")
                       {
                           @(new HtmlString("$('#jqxgridcontent').jqxGrid('hidecolumn', 'Delete');"))
                           @(new HtmlString("$('#jqxgridcontent').jqxGrid('hidecolumn', 'Edit');"))
                       }
                     }
                },
                columns:
                 [
                      { text: 'ID', editable: false, datafield: 'ID', width: 0 },
                      { text: 'Nhóm ngạch', datafield: 'NhomNgach', width: '10%' },
                      { text: 'Loại', datafield: 'Loai', width: '20%' },
                       { text: 'Hệ số góc', datafield: 'HeSoGoc', width: '10%' },
                        { text: 'Hệ số tăng', datafield: 'HeSoTang', width: '10%' },
                         { text: 'Số bậc', datafield: 'SoBac', width: '10%' },
                         { text: 'Số năm tăng lương', datafield: 'SoNamTangLuong', width: '20%' },
                      { text: 'Delete', datafield: 'Delete', columntype: 'button', pinned: true, width: '10%',
                          cellsrenderer: function () {
                              return "Delete"
                          },

                          buttonclick: function (row) {
                              // open the popup window when the user clicks a button.
                              editrow = row;

                              var dataRecord = $("#jqxgridcontent").jqxGrid('getrowdata', editrow);
                              $.get('/Gtur/GetConfirmDelete/' + dataRecord.ID, null,
                               function (data) {                                    
                                    if (data > 0) {
                                        jqxAlert.alert('Tồn mã ngạch thuộc nhóm ngạch này. Hãy xóa mã ngạch trước!');
                                        //  
                                    }
                                    else {
                                        jqxConfirm.alert('Bạn có chắc muốn xóa nhóm ngạch này khỏi danh sách?');  
                                        $("#alert_button").click(function () 
                                        {
                                            document.location.href = '/Gtur/Delete/' + dataRecord.ID + '/';
                                            jqxAlert._hide();
                                        });                                  
                                    }
                               });


                          }
                      },

                      { text: 'Edit', datafield: 'Edit', columntype: 'button', pinned: true, width: '10%',
                          cellsrenderer: function () {
                              return "Edit"
                          },

                          buttonclick: function (row) {
                              // open the popup window when the user clicks a button.
                              editrow = row;

                              var dataRecord = $("#jqxgridcontent").jqxGrid('getrowdata', editrow);
                              document.location.href = '/Gtur/Update/' + dataRecord.ID + '/';


                          }
                      }

                  ]
            });

    });
    </script>

<div id="jqxgridcontent"></div>
